//
//  MyExchangeViewController.m
//  QiYReader
//
//  Created by 奇阅科技 on 14/12/29.
//  Copyright (c) 2014年 奇阅科技. All rights reserved.
//

#import "MyExchangeViewController.h"
#import "Config.h"
#import "QYLoadingView.h"
@interface MyExchangeViewController ()<UITextFieldDelegate,UIAlertViewDelegate>
{
    QYLoadingView *loadingView;
}
@end

@implementation MyExchangeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [MobClick event:@"2015"];
    [self setInfo];
    [[ZMYNetWorkAPI default]getNewUserInfoResponse:^(NSDictionary *resultDic) {
        [self setInfo];
    } error:^(NSString *errorStr) {
    }];
}
-(void)setInfo{
    UserAccount *user =[UserAccount currentAccount];
    if (user.points.length>0) {
        self.pointsLabel.text = user.points;
        self.remainLabel.text = user.remain;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.pointsTextLabel resignFirstResponder];
}
- (IBAction)goBackButtonClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSMutableString *textStr = [NSMutableString stringWithString:textField.text];
    [textStr replaceCharactersInRange:range withString:string];
    if (textStr.length>0) {
        NSUInteger points = [textStr integerValue];
        self.remainTextField.text = [NSString stringWithFormat:@"%@",[NSNumber numberWithInteger:points/20]];
    } else {
        self.remainTextField.text = @"";
    }
    return YES;
}
- (IBAction)sureExchange:(id)sender {
    if (self.pointsTextLabel.text.length<1) {
        return;
    }
    UserAccount *user = [UserAccount currentAccount];
    if (!user) {
        [AppDelegate showHUDAndHide:@"您还未登录" view:self.view];
        return;
    }
    NSUInteger points = [self.pointsTextLabel.text integerValue];
    if (user.points.length>0) {
        if (points>[user.points integerValue]) {
            [AppDelegate showHUDAndHide:@"您要兑换的积分数量超过拥有的积分数量" view:self.view];
            return;
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                            message:[NSString stringWithFormat:@"我们将会为您兑换%@积分",[NSNumber numberWithInteger:(points/20)*20]]
                                                           delegate:self
                                                  cancelButtonTitle:@"返回"
                                                  otherButtonTitles:@"确定", nil];
            [alert show];
            return;
        }
    }
    [AppDelegate showHUDAndHide:@"您还没有积分" view:self.view];
    return;
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==1) {
        NSUInteger points = [self.pointsTextLabel.text integerValue];
        points = (points/20)*20;
        if (!loadingView) {
            loadingView = [[[NSBundle mainBundle]loadNibNamed:@"QYLoadingView" owner:self options:nil] lastObject];
        }
        [loadingView showView];
        [[ZMYNetWorkAPI default]pointsExchangeRemain:points Response:^(NSDictionary *resultDic) {
            UserAccount *user =[UserAccount currentAccount];
            [MobClick event:@"2016"];
            user.points = [NSString stringWithFormat:@"%@",[NSNumber numberWithInteger:[user.points integerValue]-points]];
            user.remain = [NSString stringWithFormat:@"%@",[NSNumber numberWithInteger:[user.remain integerValue]+points/20]];
            [UserAccount saveAccount:user];
            [self setInfo];
            [loadingView closeView];
        } error:^(NSString *errorStr) {
            [loadingView closeView];
            [AppDelegate showHUDAndHide:errorStr view:self.view];
        }];
    }
}
@end
